﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InformationSystem.Service;

namespace InformationSystem
{
    public partial class AdminPanel : Form
    {
    	
    	UserService userService;
    	
    	private SQLiteDataAdapter da;
    	
    	private DataTable dt;
    	
    	
    	ProgrammeService programmeService;
    	
    	private SQLiteDataAdapter daProgramme;
    	
    	private DataTable dtProgramme;
    	
    	ModuleService moduleService;
    	
    	private SQLiteDataAdapter daModule;
    	
    	private DataTable dtModule;
    	
        public AdminPanel()
        {
            InitializeComponent();
            
            dt = new DataTable();
            
            userService = new UserService();
            
            dtProgramme = new DataTable();
            programmeService = new ProgrammeService();
            
            
            moduleService = new ModuleService();
            dtModule = new DataTable();
            
            displayData();
            displayProgrammeData();
            displayModuleData();
            
        }

        private void AdminPanel_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        
        
        public void displayData()
		{
			
			DataTable  dtTmp  =  (DataTable)dgvStudent.DataSource;
			if(dtTmp != null)
			{
				dtTmp.Rows.Clear();
				dgvStudent.DataSource = dtTmp;
			}
			
			da = userService.getDisplayColumn();
			
			da.Fill(dt);
			
			dgvStudent.DataSource = dt;
			
			dgvStudent.Columns[0].ReadOnly = true;
			dgvStudent.Columns[3].Width = 440;
			dgvStudent.Columns[4].Width = 440;
			
		}
        
        
        public void displayProgrammeData()
		{
			
			DataTable  dtTmp  =  (DataTable)dgvProgramme.DataSource;
			if(dtTmp != null)
			{
				dtTmp.Rows.Clear();
				dgvProgramme.DataSource = dtTmp;
			}
			
			daProgramme = programmeService.getDisplayColumn();
			
			daProgramme.Fill(dtProgramme);
			
			dgvProgramme.DataSource = dtProgramme;
			
			dgvProgramme.Columns[0].ReadOnly = true;
			dgvProgramme.Columns[3].Width = 440;
			
			
		}
        
        public void displayModuleData()
		{
			
			DataTable  dtTmp  =  (DataTable)dgvModule.DataSource;
			if(dtTmp != null)
			{
				dtTmp.Rows.Clear();
				dgvModule.DataSource = dtTmp;
			}
			
			daModule = moduleService.getDisplayColumn();
			
			daModule.Fill(dtModule);
			
			dgvModule.DataSource = dtModule;
			
			dgvModule.Columns[0].ReadOnly = true;
			dgvModule.Columns[2].Width = 440;
			
			
		}
        
		void BtSaveClick(object sender, EventArgs e)
		{
			AddStudent addStudent = new AddStudent(userService);
			
			addStudent.ShowDialog();
			
			displayData();
		}
		
		void BtDelClick(object sender, EventArgs e)
		{
            if (null == dgvStudent.CurrentCell)
            {
                MessageBox.Show("Please select the row you want delete");
                return;
            }
            
            int delIndex = dgvStudent.CurrentCell.RowIndex;
			
			if(delIndex < 0)
			{
				MessageBox.Show("Please select the row you want delete");
				return;
			}
			
			dgvStudent.Rows.RemoveAt(delIndex);
			
			SQLiteCommandBuilder scb = new SQLiteCommandBuilder(da);
			da.Update(dt);
			
			MessageBox.Show("delete successful");
			
			displayData();
		}
		void BtModifyClick(object sender, EventArgs e)
		{
			if (null == dgvStudent.CurrentCell)
            {
                MessageBox.Show("Please select the row you want modify");
                return;
            }
			
			int delIndex = dgvStudent.CurrentCell.RowIndex;
			
			if(delIndex < 0)
			{
				MessageBox.Show("Please select the row you want delete");
				return;
			}
			
			AddStudent addStudent = new AddStudent(userService, Int32.Parse(dgvStudent.Rows[delIndex].Cells[0].Value.ToString()));
			
			addStudent.ShowDialog();
			
			displayData();
			
		}

        private void btAddProgramme_Click(object sender, EventArgs e)
        {
            AddProgramme addProgramme = new AddProgramme(programmeService);
            addProgramme.ShowDialog();

            displayProgrammeData();
        }

        private void btModifyProgramme_Click(object sender, EventArgs e)
        {
            if (null == dgvProgramme.CurrentCell)
            {
                MessageBox.Show("Please select the row you want modify");
                return;
            }
        	
        	int delIndex = dgvProgramme.CurrentCell.RowIndex;

            if (delIndex < 0)
            {
                MessageBox.Show("Please select the row you want modify");
                return;
            }

            AddProgramme addProgramme = new AddProgramme(programmeService, Int32.Parse(dgvProgramme.Rows[delIndex].Cells[0].Value.ToString()));
            addProgramme.ShowDialog();

            displayProgrammeData();
        }

        private void btDelProgramme_Click(object sender, EventArgs e)
        {
            if (null == dgvProgramme.CurrentCell)
            {
                MessageBox.Show("Please select the row you want delete");
                return;
            }

            int delIndex = dgvProgramme.CurrentCell.RowIndex;

            if (delIndex < 0)
            {
                MessageBox.Show("Please select the row you want delete");
                return;
            }

            dgvProgramme.Rows.RemoveAt(delIndex);

            SQLiteCommandBuilder scb = new SQLiteCommandBuilder(daProgramme);
            daProgramme.Update(dtProgramme);

            MessageBox.Show("delete successful");

            displayProgrammeData();
        }
		void BtAddModuleClick(object sender, EventArgs e)
		{
			AddModule addModule = new AddModule(moduleService);
            addModule.ShowDialog();

            displayModuleData();
		}
		void BtModifyModuleClick(object sender, EventArgs e)
		{
			if (null == dgvModule.CurrentCell)
            {
                MessageBox.Show("Please select the row you want modify");
                return;
            }
			
			int delIndex = dgvModule.CurrentCell.RowIndex;

            if (delIndex < 0)
            {
                MessageBox.Show("Please select the row you want modify");
                return;
            }

            AddModule addModule = new AddModule(moduleService, Int32.Parse(dgvModule.Rows[delIndex].Cells[0].Value.ToString()));
            addModule.ShowDialog();

            displayModuleData();
		}
		void BtDelModuleClick(object sender, EventArgs e)
		{
            if (null == dgvModule.CurrentCell)
            {
                MessageBox.Show("Please select the row you want delete");
                return;
            }
            
            int delIndex = dgvModule.CurrentCell.RowIndex;

            if (delIndex < 0)
            {
                MessageBox.Show("Please select the row you want delete");
                return;
            }

            dgvModule.Rows.RemoveAt(delIndex);

            SQLiteCommandBuilder scb = new SQLiteCommandBuilder(daModule);
            daModule.Update(dtModule);

            MessageBox.Show("delete successful");

            displayModuleData();
		}
        
    }
}
